package com.ielts.activity;

import vn.com.baseapplication.activity.FBBaseLoginActivity;
import vn.com.baseapplication.constant.BaseConstant;
import vn.com.baseapplication.util.FBUtils;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.facebook.Response;
import com.facebook.Session;
import com.facebook.SessionState;
import com.facebook.model.GraphUser;
import com.ielts.IeltsConstant;
import com.ielts.services.IeltsServices;

public class LoginActivity extends FBBaseLoginActivity {
	Button btnLogin, btnLogout;
	TextView tvLog;
	IeltsServices mService;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_login);

		btnLogin = (Button) findViewById(R.id.login_btn_fb);
		btnLogout = (Button) findViewById(R.id.logout_btn_fb);
		tvLog = (TextView) findViewById(R.id.welcome);
		btnLogin.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				connectFacebookAccount();
			}
		});

		btnLogout.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				disconnectFacebookAccount();
			}
		});

		btnLogout.setVisibility(View.GONE);
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		if(!isServiceConnected()){
        	bindBaseService(IeltsServices.class);
        }
	}
	
	@Override
	public void onServiceConnected() {
		super.onServiceConnected();
		mService = (IeltsServices)getBaseServices();
		
	}

	@Override
	public void onCompleted(GraphUser user, Response response) {
		if (user != null) {
			String userFbId = user.getId();
			String userFbLink = user.getLink();
			String name = user.getName();
			String email = user.asMap().get("email").toString();
//			Log.e("User info", userFbId+"-"+name+"-"+email);
			
			IeltsApplication.getInstance().setValue(
					IeltsConstant.KEY_USER_NAME, name);
			IeltsApplication.getInstance().setValue(
					IeltsConstant.KEY_FB_LINK_ID, userFbLink);
			IeltsApplication.getInstance().setValue(
					IeltsConstant.KEY_FB_USER_ID, userFbId);
			IeltsApplication.getInstance().setValue(
					IeltsConstant.KEY_USER_EMAIL, email);
			
//			btnLogout.setVisibility(View.VISIBLE);
			btnLogin.setVisibility(View.GONE);

			tvLog.setText("welcome: " + name);
			// register user
			mService.registerUserPost();
		} else {
			btnLogout.setVisibility(View.GONE);
			btnLogin.setVisibility(View.VISIBLE);

			tvLog.setText("You're not login.");
		}
	}

	@Override
	public void onFailed(Session session, SessionState state,
			Exception exception) {
		FBUtils.getHashKeySentToFB(this, "com.ielts.activity");
	}
	
	@Override
	public void onReceive(Context context, Intent intent) {
		if(!intent.hasExtra(BaseConstant.MSG_FLAG)){
			return;
		}
		String FLAG = intent.getStringExtra(BaseConstant.MSG_FLAG);
		if(FLAG!=null && FLAG.equals(IeltsConstant.REGISTER_USER+"")){
			String result = intent.getStringExtra(BaseConstant.MSG_RESULT);
			if(result.equals(BaseConstant.MSG_RESULT_OK)){
				this.finish();
				startActivity(new Intent(this, MainActivity.class));
			}
			else if(result.equals(BaseConstant.MSG_RESULT_FAILED)){
				Toast.makeText(this, "Register failed.", Toast.LENGTH_SHORT).show();
			}
		}
	}
}
